﻿using UnityEngine;
using System.Collections;

public class RocktasticIsBorn : MonoBehaviour {

	public GameObject  rock;

	public Vector3 direction = new Vector3(-1,0,0);

	int torqueLimit = 4;
	public int angleRangePositiveValue = 100;
	public int angleRangeNegativeValue = -100;
	public float angleScale = 10.0f;

	public int speedRangeTopValue = 10;
	public int speedRangeBottomValue = 8;
	public float speedScale = 10.0f;

	public int positionRangeTopValue = 100;
	public int positionRangeBottomValue = -100;
	public float positionScale = 100.0f;

	public int spawnRangeTopValue = 5;
	public int spawnRangeBottomValue = 4;

	public int quantity = 1;

	public int reSpawnsBase = 3;
	int reSpawns;

	void Start () {
	}

	public void StartTheHuge() {
		reSpawns = reSpawnsBase;
		SpawnTheHuge ();
	}

	void SpawnTheHuge() {
		for (int i = 0; i < quantity; i++) {

			float speed = ((float)Random.Range(speedRangeBottomValue, speedRangeTopValue))/speedScale;
			float angleValue =  ((float)Random.Range(angleRangeNegativeValue, angleRangePositiveValue))/angleScale;

			GameObject copy = (GameObject)Instantiate (rock, transform.position, transform.rotation);
			Rigidbody2D huge = copy.GetComponent<Rigidbody2D>();
			huge.transform.Translate(new Vector3( ((float)Random.Range(positionRangeBottomValue, positionRangeTopValue)/positionScale), ((float)Random.Range(positionRangeBottomValue, positionRangeTopValue))/positionScale, 0));

			Vector3 rotatedDirection = Quaternion.AngleAxis(angleValue, Vector3.forward) * direction;
			huge.velocity = transform.TransformDirection(rotatedDirection.normalized * speed);
			huge.AddTorque(Random.Range (-torqueLimit, torqueLimit), ForceMode2D.Impulse);
		}
		reSpawns--;
		if (reSpawns < 0) {
			Destroy (gameObject);
			return;
		}
		Invoke("SpawnTheHuge", Random.Range(spawnRangeBottomValue, spawnRangeTopValue));
	}
}
